package com.ict.web.utils.pdf;

import com.ict.common.exception.ServiceException;
import com.ict.common.utils.SecurityUtils;
import com.ict.common.utils.StringUtils;
import com.ict.system.mapper.SysUserRoleMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xc
 */
@Component
public class RoleUtil {
    @Resource
    private SysUserRoleMapper userRoleMapper;

    public static RoleUtil roleUtil;

    @PostConstruct
    public void init() {
        roleUtil = this;
        roleUtil.userRoleMapper = this.userRoleMapper;
    }
    /**
     * 判断用户角色
     *
     * @param roleId 角色id
     * @return 结果
     */
    public static boolean getUserRole(Long roleId)
    {
        List<Long> userIds = roleUtil.userRoleMapper.getUserIdByRoleId(roleId);
        Long userId = SecurityUtils.getUserId();
        try {
            List<Long> projectUnit = userIds.stream().filter(s -> s.equals(userId)).collect(Collectors.toList());
            if(StringUtils.isNotEmpty(projectUnit)){
                return true;
            }
        }catch (Exception e){
            throw new ServiceException("获取用户角色异常");
        }
        return false;
    }
}
